namespace SmartSoft.ADP
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CTSXDataAccess
    {
        private WSCTSXDataAccess.WSCTSXDataAccess wDataAcess = new WSCTSXDataAccess.WSCTSXDataAccess();
        private SmartSoft.DAL.CTSXDataAccess oDataAcess = new SmartSoft.DAL.CTSXDataAccess();

        public CTSXDataAccess()
        {
            wDataAcess.Url = wDataAcess.Url.Replace("localhost", AdapterInstance.ServerIp);
        }

        public int CheckInvoiceLock(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.CheckInvoiceLock(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
            else
            {
                return oDataAcess.CheckInvoiceLock(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
        }

        public int CheckInvoicePost(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.CheckInvoicePost(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
            else
            {
                return oDataAcess.CheckInvoicePost(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
        }

        public int CheckInvoiceStatus(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.CheckInvoiceStatus(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
            else
            {
                return oDataAcess.CheckInvoiceStatus(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
        }

        public object Cong_So_Ct(string ps_Subsidiary, string ps_InvoiceCode, object pd_Ngay_Ct, object ps_So_Ct)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.Cong_So_Ct(ps_Subsidiary, ps_InvoiceCode, pd_Ngay_Ct, ps_So_Ct == DBNull.Value ? string.Empty : ps_So_Ct);
            }
            else
            {
                return oDataAcess.Cong_So_Ct(ps_Subsidiary, ps_InvoiceCode, pd_Ngay_Ct, ps_So_Ct);
            }
        }

        public int CtSX_Pause_Delete(object ps_MasterKey)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.CtSX_Pause_Delete(ps_MasterKey);
            }
            else
            {
                return oDataAcess.CtSX_Pause_Delete(ps_MasterKey);
            }
        }

        public object CtSX_Pause_Get(object ps_MasterKey)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.CtSX_Pause_Get(ps_MasterKey);
            }
            else
            {
                return oDataAcess.CtSX_Pause_Get(ps_MasterKey);
            }
        }

        public int CtSX_Pause_Save(object ps_MasterKey, object pd_Ngay_Huy)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.CtSX_Pause_Save(ps_MasterKey, pd_Ngay_Huy);
            }
            else
            {
                return oDataAcess.CtSX_Pause_Save(ps_MasterKey, pd_Ngay_Huy);
            }
        }

        public bool Delete(string ps_UserName, object po_KeyCode)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.Delete(ps_UserName, po_KeyCode == DBNull.Value ? string.Empty : po_KeyCode);
            }
            else
            {
                return oDataAcess.Delete(ps_UserName, po_KeyCode);
            }
        }

        //public bool DeleteDetailDataRow(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_KeyCode)
        //{
        //    if (AdapterInstance.IsRemoteConnected)
        //    {
        //        return wDataAcess.DeleteDetailDataRow(po_Connection, po_Transaction, po_KeyCode == DBNull.Value ? string.Empty : po_KeyCode);
        //    }
        //    else
        //    {
        //        return oDataAcess.DeleteDetailDataRow(po_Connection, po_Transaction, po_KeyCode);
        //    }
        //}

        public DataTable GetDetailCommand(object ps_MasterKey)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetDetailCommand(ps_MasterKey);
            }
            else
            {
                table= oDataAcess.GetDetailCommand(ps_MasterKey);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataSet GetDetailDataSet(string ps_InvoiceOrder)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.GetDetailDataSet(ps_InvoiceOrder);
            }
            else
            {
                return oDataAcess.GetDetailDataSet(ps_InvoiceOrder);
            }
        }

        public DataTable GetMasterCommand(object ps_ParentKey)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetMasterCommand(ps_ParentKey);
            }
            else
            {
                table= oDataAcess.GetMasterCommand(ps_ParentKey);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataRow GetMasterDataRow(string ps_InvoiceOrder)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return SmartSoft.DAL.SSDataAccess.ConvertTableToRow(wDataAcess.GetMasterDataRow(ps_InvoiceOrder));
            }
            else
            {
                return oDataAcess.GetMasterDataRow(ps_InvoiceOrder);
            }
        }

        public DataTable GetMasterTable(string ps_Subsidiary, int pi_DaysFilter, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object ps_So_Ct1, object ps_So_Ct2, object ps_Ma_Ct, object ps_Ma_YC, object ps_Ma_NN, object ps_Ma_Bp, object ps_Ma_Hd, object ps_Ma_Vt, object ps_Dien_Giai, object ps_Ma_Loai1, object ps_Ma_Loai2, object ps_Ma_Loai3, object ii_FilterByUser, string ps_UserName, object ps_Notes, int pi_Posted, int pi_Locked)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetMasterTable(ps_Subsidiary, pi_DaysFilter, pd_Ngay_Ct1, pd_Ngay_Ct2, ps_So_Ct1, ps_So_Ct2, ps_Ma_Ct, ps_Ma_YC, ps_Ma_NN, ps_Ma_Bp, ps_Ma_Hd, ps_Ma_Vt, ps_Dien_Giai, ps_Ma_Loai1, ps_Ma_Loai2, ps_Ma_Loai3, ii_FilterByUser, ps_UserName, ps_Notes, pi_Posted, pi_Locked);
            }
            else
            {
                table= oDataAcess.GetMasterTable(ps_Subsidiary, pi_DaysFilter, pd_Ngay_Ct1, pd_Ngay_Ct2, ps_So_Ct1, ps_So_Ct2, ps_Ma_Ct, ps_Ma_YC, ps_Ma_NN, ps_Ma_Bp, ps_Ma_Hd, ps_Ma_Vt, ps_Dien_Giai, ps_Ma_Loai1, ps_Ma_Loai2, ps_Ma_Loai3, ii_FilterByUser, ps_UserName, ps_Notes, pi_Posted, pi_Locked);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public void Lock(string ps_UserName, object ps_InvoiceOrder, bool pb_Locked)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                wDataAcess.Lock(ps_UserName, ps_InvoiceOrder, pb_Locked);
            }
            else
            {
                oDataAcess.Lock(ps_UserName, ps_InvoiceOrder, pb_Locked);
            }
        }

        public bool NumberExisted(string ps_Subsidiary, bool pb_CheckNewRow, string ps_InvoiceOrder, string ps_InvoiceCode, object pd_Ngay_Ct, object ps_So_Ct)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.NumberExisted(ps_Subsidiary, pb_CheckNewRow, ps_InvoiceOrder, ps_InvoiceCode, pd_Ngay_Ct, ps_So_Ct == DBNull.Value ? string.Empty : ps_So_Ct);
            }
            else
            {
                return oDataAcess.NumberExisted(ps_Subsidiary, pb_CheckNewRow, ps_InvoiceOrder, ps_InvoiceCode, pd_Ngay_Ct, ps_So_Ct);
            }
        }

        public bool Post(string ps_UserName, object ps_InvoiceOrder, object pi_Posted)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.Post(ps_UserName, ps_InvoiceOrder, pi_Posted);
            }
            else
            {
                return oDataAcess.Post(ps_UserName, ps_InvoiceOrder, pi_Posted);
            }
        }

        public bool Save(bool pb_InvoiceHistory, bool pb_NewChecked, string ps_Subsidiary, string ps_UserName, DataRow po_MasterDataRow, DataTable po_DetailTable)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.Save(pb_InvoiceHistory, pb_NewChecked, ps_Subsidiary, ps_UserName, SmartSoft.DAL.SSDataAccess.ConvertRowToTable(po_MasterDataRow), po_DetailTable);
            }
            else
            {
                return oDataAcess.Save(pb_InvoiceHistory, pb_NewChecked, ps_Subsidiary, ps_UserName, po_MasterDataRow, po_DetailTable);
            }
        }

        //public int SaveDetailDataRow(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_MasterKey, DataRow row)
        //{
        //    if (AdapterInstance.IsRemoteConnected)
        //    {
        //        return wDataAcess.SaveDetailDataRow(po_Connection, po_Transaction, po_MasterKey, row);
        //    }
        //    else
        //    {
        //        return oDataAcess.SaveDetailDataRow(po_Connection, po_Transaction, po_MasterKey, row);
        //    }
        //}

        //public int SaveDetailMaterial(SqlConnection po_Connection, SqlTransaction po_Transaction, bool pb_NewChecked, string ps_Subsidiary, DataRow row)
        //{
        //    if (AdapterInstance.IsRemoteConnected)
        //    {
        //        return wDataAcess.SaveDetailMaterial(po_Connection, po_Transaction, pb_NewChecked, ps_Subsidiary, row);
        //    }
        //    else
        //    {
        //        return oDataAcess.SaveDetailMaterial(po_Connection, po_Transaction, pb_NewChecked, ps_Subsidiary, row);
        //    }
        //}

        //public int SaveMasterDataRow(SqlConnection po_Connection, SqlTransaction po_Transaction, bool pb_NewChecked, string ps_Subsidiary, DataRow row)
        //{
        //    if (AdapterInstance.IsRemoteConnected)
        //    {
        //        return wDataAcess.SaveMasterDataRow(po_Connection, po_Transaction, pb_NewChecked, ps_Subsidiary, row);
        //    }
        //    else
        //    {
        //        return oDataAcess.SaveMasterDataRow(po_Connection, po_Transaction, pb_NewChecked, ps_Subsidiary, row);
        //    }
        //}
    }
}
